<?php
// $Id$

/**
 * Controller_Enroll 控制器
 */
class Controller_Enroll extends Controller_Abstract
{

	function actionIndex()
	{
		$page = (int)$this->_context->page;
		$limit = 5;
		$q = Enroll::find()->limitPage($page, $limit);

		$this->_view['pager'] = $q->getPagination();
		$this->_view['list'] = $q->getAll();
		$this->_view['start'] = $page*$limit;
		$this->_view['subject'] = "入学批次";
	}

	function actionCreate()
	{
		// 构造表单对象
		$form = new Form_Enroll('');
	 
		if ($this->_context->isPOST() && $form->validate($_POST))
		{
			// 创建 user 对象并保存
			$enroll = new Enroll($form->values());
			$enroll->save();
			// 创建成功后，重定向浏览器
			return $this->_redirect(url('enroll'));
		}
	 
		// 将表单对象传递给视图
		$this->_view['form'] = $form;
		$this->_view['subject'] = "入学批次";
	}

	function actionEdit()
	{
		// 查询指定 ID
		$id = $this->_context->id;
		$enroll = Enroll::find('id = ?', $id)->query();
	 
		// 构造表单对象
		$form = new Form_Enroll('');
		// 修改表单标题
		$this->_view['subject'] = "入学批次";
	 
		if ($this->_context->isPOST() && $form->validate($_POST))
		{
			// changeProps() 方法可以批量修改对象的属性，但不会修改只读属性的值
			$enroll->changeProps($form->values());
	 
			// 保存并重定向浏览器
			$enroll->save();
			return $this->_redirect(url('enroll'));
		}
		elseif (!$this->_context->isPOST())
		{
			// 如果不是 POST 提交，则把对象值导入表单
			$form->import($enroll);
		}
	 
		$this->_view['form'] = $form;
		// 重用 create 动作的视图
		$this->_viewname = 'create';

	}
	
	function actionDelete()
	{
		// destroyWhere() 方法的参数和 find() 方法完全一致
		Enroll::meta()->destroyWhere('id = ?', $this->_context->id);
	 
		return $this->_redirect(url('enroll'));
	}

}


